package cn.iocoder.yudao.module.system.controller.admin.usershops;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.usershops.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.usershops.UserShopsDO;
import cn.iocoder.yudao.module.system.service.usershops.UserShopsService;

@Tag(name = "管理后台 - 用户店铺关联")
@RestController
@RequestMapping("/system/user-shops")
@Validated
public class UserShopsController {

    @Resource
    private UserShopsService userShopsService;

    @PostMapping("/create")
    @Operation(summary = "创建用户店铺关联")
    @PreAuthorize("@ss.hasPermission('system:user-shops:create')")
    public CommonResult<Long> createUserShops(@Valid @RequestBody UserShopsSaveReqVO createReqVO) {
        return success(userShopsService.createUserShops(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户店铺关联")
    @PreAuthorize("@ss.hasPermission('system:user-shops:update')")
    public CommonResult<Boolean> updateUserShops(@Valid @RequestBody UserShopsSaveReqVO updateReqVO) {
        userShopsService.updateUserShops(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户店铺关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:user-shops:delete')")
    public CommonResult<Boolean> deleteUserShops(@RequestParam("id") Long id) {
        userShopsService.deleteUserShops(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户店铺关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:user-shops:query')")
    public CommonResult<UserShopsRespVO> getUserShops(@RequestParam("id") Long id) {
        UserShopsDO userShops = userShopsService.getUserShops(id);
        return success(BeanUtils.toBean(userShops, UserShopsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户店铺关联分页")
    @PreAuthorize("@ss.hasPermission('system:user-shops:query')")
    public CommonResult<PageResult<UserShopsRespVO>> getUserShopsPage(@Valid UserShopsPageReqVO pageReqVO) {
        PageResult<UserShopsDO> pageResult = userShopsService.getUserShopsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserShopsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户店铺关联 Excel")
    @PreAuthorize("@ss.hasPermission('system:user-shops:export')")
    @OperateLog(type = EXPORT)
    public void exportUserShopsExcel(@Valid UserShopsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserShopsDO> list = userShopsService.getUserShopsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户店铺关联.xls", "数据", UserShopsRespVO.class,
                        BeanUtils.toBean(list, UserShopsRespVO.class));
    }

}